from SequenceJob import SequenceJob
from Job import  Job, JobResult, JobStatus
from Trigger import JobTrigger
import unittest

class SequencejobTest(unittest.TestCase):
    def setUp(self):
        self.sequencejob = SequenceJob()
    
    def testAddJob(self):
        self.sequencejob.addJob('haha', Job())
        self.sequencejob.addJob('haha', Job())
        self.sequencejob.addJob('haha', Job())
        self.assertEqual(len(self.sequencejob.job_list), 1)
        
        
    def testRemoveJob(self):
        self.sequencejob.addJob('haha', Job())
        self.sequencejob.removeJob('haha')
        self.assertEqual(len(self.sequencejob.job_list), 0)



    def testtriggerJob(self):
        self.sequencejob.addJob('1', Job())
        self.sequencejob.addJob('2', Job())
        self.sequencejob.addJob('3', Job())
        
        t1 = JobTrigger()
        t1.addCondition([['main', JobResult.Success]], [])
        self.sequencejob.setTirgger('1', t1)
        
        t2 = JobTrigger()
        t2.addCondition([['1', JobResult.Success]], [])
        self.sequencejob.setTirgger('2', t2)
        
        t3 = JobTrigger()
        t3.addCondition([['2', JobResult.Error]], [])
        self.sequencejob.setTirgger('3', t3)
        
        self.sequencejob.start()
        
        self.assertEqual(self.sequencejob.job_list['1'].Status(), JobStatus.WaitingToRun)
        self.sequencejob.job_list['1'].job.start()
        self.sequencejob.job_list['1'].job.stop(JobResult.Success)
        
        self.assertEqual(self.sequencejob.job_list['2'].Status(), JobStatus.WaitingToRun)
        self.sequencejob.job_list['2'].job.start()
        self.sequencejob.job_list['2'].job.stop(JobResult.Success)
        
        self.assertEqual(self.sequencejob.job_list['3'].Status(), JobStatus.Canceled)
        
        self.assertEqual(self.sequencejob.status, JobStatus.RunToCompletion)
        
if __name__ == '__main__':
    unittest.main()
    print('ok')